package cn.huaguo.trigger.http;

import cn.huaguo.domain.order.model.entity.OrderEntity;
import cn.huaguo.domain.order.model.valobj.OrderStatusVO;
import cn.huaguo.domain.order.service.IOrderService;
import cn.huaguo.domain.user.service.IUserService;
import cn.huaguo.trigger.http.dto.AddOrderReqDTO;
import cn.huaguo.types.context.BaseContext;
import cn.huaguo.types.enums.ResponseCode;
import cn.huaguo.types.model.Response;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：Arvin
 * @description：Ordercontroller
 * @Date：2024/5/14 13:51
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private IOrderService OrderService;
    @Resource
    private IUserService userService;

    /**
     * 新增订单
     * TODO 待废弃 订单不允许单独新增
     *
     * @param addOrderReqDTO
     * @return
     */
    @PostMapping("/addOrder")
    public Response addOrder(@RequestBody AddOrderReqDTO addOrderReqDTO) {
        try {
            /*String str = "00";
            if () {
                log.info("新增订单info:{}", JSON.toJSONString(addOrderReqDTO));
                OrderService.addOrder(OrderEntity.builder()
                        .taskId(addOrderReqDTO.getTaskId())
                        //.orderStatus(OrderStatusConstant.ORDER_RECEIVED)
                        .build()
                );
                log.info("新增订单成功:{}", JSON.toJSONString(addOrderReqDTO));

                return Response.builder()
                        .code(ResponseCode.SUCCESS.getCode())
                        .info(ResponseCode.SUCCESS.getInfo())
                        .build();
            } else {

            }
        */
            log.error("新增订单失败：{}", JSON.toJSONString(addOrderReqDTO));
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getInfo())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("新增订单失败：{} message: {}", JSON.toJSONString(addOrderReqDTO), e.getMessage());
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getInfo())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 查询当前用户相关订单列表
     */
    @GetMapping("/orderList")
    public Response<List<OrderEntity>> queryOrderInfo() {
        List<OrderEntity> orderEntityList = null;
        try {
            String userId = BaseContext.getCurrentId();
            orderEntityList = OrderService.queryOrderInfoById(userId);
            log.info("查询当前用户相关订单列表:{}", orderEntityList);
            return Response.<List<OrderEntity>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(orderEntityList)
                    .build();
        } catch (Exception e) {
            log.error("查询失败当前订单信息: {}, message: {}", orderEntityList, e.getMessage());
            return Response.<List<OrderEntity>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 订单完成
     *
     * @param taskId
     * @return
     */
    @PutMapping("/finish/{taskId}")
    public Response<Boolean> finishOrder(@PathVariable("taskId") String taskId) {
        try {
            log.info("订单状态更新");
            String orderStatus = OrderStatusVO.FINISH.getCode();
            Boolean result = OrderService.updateOrderStatus(taskId, orderStatus);

            log.info("订单状态更新成功：{}", result);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(result)
                    .build();
        } catch (Exception e) {
            log.error("订单状态更新失败：{}", e.getMessage());
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : ResponseCode.UN_ERROR.getInfo())
                    .data(false)
                    .build();
        }
    }

    /**
     * 订单取消退款
     *
     * @param taskId
     * @return
     */
    @PutMapping("/refund/{taskId}")
    public Response<Boolean> refund(@PathVariable("taskId") String taskId) {
        try {
            log.info("订单退款");
            String orderStatus = OrderStatusVO.REFUND.getCode();
            Boolean result = OrderService.refund(taskId, orderStatus);

            log.info("订单退款：{}", result);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(result)
                    .build();
        } catch (Exception e) {
            log.error("订单退款：{}", e.getMessage());
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : ResponseCode.UN_ERROR.getInfo())
                    .data(false)
                    .build();
        }
    }

}
